package com.cw.controller;

import com.cw.beans.*;
import com.cw.mapper.*;
import com.cw.service.AssetServiceImpl;
import com.cw.util.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.rmi.MarshalledObject;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AssetController {
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private   DeptMapper deptMapper;
    @Autowired
    private  TypeMapper typeMapper;
    @Autowired
    private    DepreciateMapper depreciateMapper;
    @Autowired
    private   StateMapper stateMapper;
    @Autowired
    private OutOfStorageMapper outOfStorageMapper;
     @ResponseBody
    @RequestMapping("/home")
    public Object asset(){
        Map map =  new HashMap();
        int upAsset = assetMapper.getUpAsset();

        map.put("upAsset",upAsset);
        ArrayList<Asset> fiveAsset = assetMapper.getFiveAsset();
        ArrayList<Zc_type_num> assetByByte = assetMapper.getAssetByByte();
        map.put("assetByByte",assetByByte);
      int allAssetSum = assetMapper.getAllAssetSum();
        int noUseAsset = assetMapper.getNoUseAsset();
        int useAsset = assetMapper.getUseAsset();

        map.put("useAsset",useAsset);
        map.put("fiveAsset",fiveAsset);
        map.put("upAsset",upAsset);
        map.put("allAssetSum",allAssetSum);
        map.put("noUseAsset",noUseAsset);
    DecimalFormat df = new DecimalFormat("0.00");
        int[] monthMany = new int[12];
        double[]monthManyZen = new double[12];
         String[] a = {"2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"};
        for (int i = 0; i < a.length; i++) {
            monthMany[i] = assetMapper.getMonthMany(a[i]);
        }
        for (int i = 0; i < monthMany.length; i++) {
            if(i==0){
                monthManyZen[i] = 0.0;
            }
            else {
                double c = ((float) monthMany[i]/(float) monthMany[i-1]-1)*100;

                c = Double.parseDouble(df.format(c));

              // c= Math.round(c*10000)*0.01;

                monthManyZen[i] = c;

            }
        }
        map.put("monthMany",monthMany);
        map.put("monthManyZen",monthManyZen);
        return map;
    }
    @ResponseBody
    @RequestMapping("/getlist")
    public Object asset(String pagesize,String name,String type,String dept,String state){
    Map map = new HashMap();
Asset  asset = new Asset();
if(name !=null&& !name.equals("")){
asset.setName(name);
}
if (type !=null && !type.equals("")){
asset.setType_id(Integer.parseInt(type));
}
if (dept !=null && !dept.equals("")){
asset.setDept_id(Integer.parseInt(dept));
}
if (state !=null && !state.equals("")){
asset.setState_id(Integer.parseInt(state));

}
        Page page =new Page();
        page.setCurrentPage(pagesize==null?1:Integer.parseInt(pagesize));
        //得到有多少
        ArrayList<Asset> allAssets = assetMapper.getAllAssets(asset, page);

        if(allAssets.size()==0){
            map.put("mag","没有数据");
             Asset asset1 = new Asset();
//             allAssets = assetMapper.getAllAssets(asset1, page);
//             map.put("list",allAssets);
             map.put("page",page);
             map.put("asset",new Asset());

            map.put("dept",deptMapper.getAllDept());

            map.put("type",typeMapper.gettype());

            map.put("state",stateMapper.getState());
             return map;
        }else {
            map.put("list",allAssets);
            int   num = assetMapper.getAllAssets1(asset);
            page.setRecordTotal(num);
            map.put("page",page);
            map.put("type",typeMapper.gettype());
            map.put("dept",deptMapper.getAllDept());
            map.put("state",stateMapper.getState());
            map.put("asset",asset);

            return map;
        }



}

//资产入库提交
    @RequestMapping("/setAsset_Inbound")
    public String setAsset_Inbound(String name, Integer type_id, Integer dept_id,
                                   Integer depreciate_id, String specifications,String acquisition_date,
                                   Float original_value,String storage_location,String description,
                                   String supplier,Integer term_of_service){
        Asset asset = new Asset();
        asset.setName(name);
        asset.setType_id(type_id);
        asset.setDept_id(dept_id);
        asset.setDepreciate_id(depreciate_id);
        asset.setSpecifications(specifications);
        asset.setAcquisition_date(acquisition_date);
        asset.setOriginal_value(original_value);
        asset.setStorage_location(storage_location);
        asset.setDescription(description);
        asset.setSupplier(supplier);
        asset.setTerm_of_service(term_of_service);
//        asset.setState_id(state_id);
        //提交
        assetMapper.setAsset(asset);
//        获取数据
        //更改相关数据 获取asset总条数
        int a = assetMapper.getAllAssetSum();
        System.out.println(a);
        OutOfStorage outOfStorage = new OutOfStorage();
        outOfStorage.setAsset_id(a+1);
        outOfStorage.setStorage_date(acquisition_date);
        outOfStorageMapper.setOutOfStorage(outOfStorage);

        return "Asset_Inbound";
    }

    @ResponseBody
    @RequestMapping("/getAssetDept")
    public Object getAssetDept(String dept_id){
        Map map = new HashMap();
        Integer num = assetMapper.getnum(Integer.valueOf(dept_id));
        System.out.println(num);
        Integer value = assetMapper.getvalue(Integer.valueOf(dept_id));
        System.out.println(value);
        Integer num1 = assetMapper.getnum1(Integer.valueOf(dept_id));
        Integer value1 = assetMapper.getvalue1(Integer.valueOf(dept_id));
        Integer num2 = assetMapper.getnum2(Integer.valueOf(dept_id));
        Integer value2 = assetMapper.getvalue2(Integer.valueOf(dept_id));


        map.put("dept_num", num);
        map.put("dept_value",value);
        map.put("dept_num1", num1);
        map.put("dept_value1",value1);
        map.put("dept_num2", num2);
        map.put("dept_value2",value2);

        System.out.println(map);
        return map;
    }
}
